package com.idbk.chargestation.api;

import com.idbk.chargestation.AppContext;
import com.idbk.chargestation.bean.JsonHistory;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.request.RequestCall;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class APIForFile {

    /**
     * 上传用户头像
     *
     * @param file     头像文件
     * @param callback 回调
     */
    public static RequestCall uploadHeaderImage(
            File file,
            Callback<String> callback) {

        AppContext appContext = AppContext.getInstance();
        String token = appContext.getToken();

        final String URL = ChargeStationURL.format("/user/upload.do");

        Map<String, String> params = new HashMap<String, String>();
        params.put("token", token);

        Map<String, String> headers = new HashMap<>();

        RequestCall request = OkHttpUtils.post()
                .addFile("upload", file.getName(), file)//
                .url(URL)
                .params(params)
                .headers(headers)
                .build();
        request.execute(callback);
        return request;
    }

    /**
     * 车主认证信息提交
     *
     * @param vehicleBrandId 汽车品牌ID
     * @param vehicleTypeId  汽车型号ID
     * @param vehicleNum     汽车车牌号
     * @param engineNum      汽车发动机编号后六位
     * @param chassisNum     车架号后六位
     * @param chargePortType 充电接口类型
     * @param file           上传的行驶证照片图像
     */
    public static RequestCall addCarMessage(
            int vehicleBrandId,
            int vehicleTypeId,
            String vehicleNum,
            String engineNum,
            String chassisNum,
            int chargePortType,
            File file,
            Callback<String> callback) {
        final String URL = ChargeStationURL.format("/user/vehicleowner!addInfo.do");

        Map<String, String> params = new HashMap<String, String>();
        AppContext appContext = AppContext.getInstance();
        params.put("model.vehicleBrandId", vehicleBrandId + "");
        params.put("model.vehicleTypeId", vehicleTypeId + "");
        params.put("model.vehicleNum", vehicleNum);
        params.put("model.engineNum", engineNum);
        params.put("model.chassisNum", chassisNum);
        params.put("model.chargePortType", chargePortType + "");
        params.put("token", appContext.getToken());

        Map<String, String> headers = new HashMap<>();

        RequestCall request = OkHttpUtils.post()
                .addFile("upload", file.getName(), file)//
                .url(URL)
                .params(params)
                .headers(headers)
                .build();
        request.execute(callback);
        return request;
    }

    /**
     * 车主认证信息修改
     *
     * @param id             车主认证信息的id
     * @param vehicleBrandId 汽车品牌ID
     * @param vehicleTypeId  汽车型号ID
     * @param vehicleNum     汽车车牌号
     * @param engineNum      汽车发动机编号后六位
     * @param chassisNum     车架号后六位
     * @param chargePortType 充电接口类型
     * @param file           上传的行驶证照片图像
     */
    public static RequestCall modifyCarMessage(
            int id,
            int vehicleBrandId,
            int vehicleTypeId,
            String vehicleNum,
            String engineNum,
            String chassisNum,
            int chargePortType,
            File file,
            Callback<String> callback) {
        final String URL = ChargeStationURL.format("/user/vehicleowner!addInfo.do");

        Map<String, String> params = new HashMap<String, String>();
        AppContext appContext = AppContext.getInstance();
        params.put("model.id", id + "");
        params.put("model.vehicleBrandId", vehicleBrandId + "");
        params.put("model.vehicleTypeId", vehicleTypeId + "");
        params.put("model.vehicleNum", vehicleNum);
        params.put("model.engineNum", engineNum);
        params.put("model.chassisNum", chassisNum);
        params.put("model.chargePortType", chargePortType + "");
        params.put("token", appContext.getToken());

        Map<String, String> headers = new HashMap<>();

        RequestCall request = OkHttpUtils.post()
                .addFile("upload", file.getName(), file)//
                .url(URL)
                .params(params)
                .headers(headers)
                .build();
        request.execute(callback);
        return request;
    }

    /**
     * 车主认证信息修改(不提交照片)
     *
     * @param id             车主认证信息的id
     * @param vehicleBrandId 汽车品牌ID
     * @param vehicleTypeId  汽车型号ID
     * @param vehicleNum     汽车车牌号
     * @param engineNum      汽车发动机编号后六位
     * @param chassisNum     车架号后六位
     * @param chargePortType 充电接口类型
     */
    public static RequestCall modifyCarMessageNoPic(
            int id,
            int vehicleBrandId,
            int vehicleTypeId,
            String vehicleNum,
            String engineNum,
            String chassisNum,
            int chargePortType,
            Callback<String> callback) {
        final String URL = ChargeStationURL.format("/user/vehicleowner!addInfo.do");

        Map<String, String> params = new HashMap<String, String>();
        AppContext appContext = AppContext.getInstance();
        params.put("model.id", id + "");
        params.put("model.vehicleBrandId", vehicleBrandId + "");
        params.put("model.vehicleTypeId", vehicleTypeId + "");
        params.put("model.vehicleNum", vehicleNum);
        params.put("model.engineNum", engineNum);
        params.put("model.chassisNum", chassisNum);
        params.put("model.chargePortType", chargePortType + "");
        params.put("token", appContext.getToken());

        RequestCall request = OkHttpUtils
                .post()
                .url(URL)
                .params(params)
                .build();
        request.execute(callback);
        return request;
    }


    /**
     * 充电点采集
     *
     * @param data JsonHistory数据
     * @param pic1 充电点实景图
     */
    public static RequestCall chargeCollection(
            JsonHistory data,
            File pic1,
            File pic2,
            File pic3,
            Callback<String> callback) {
        final String URL = ChargeStationURL.format("/other/otherpile.do");

        Map<String, String> params = new HashMap<String, String>();
        AppContext appContext = AppContext.getInstance();
        params.put("token", appContext.getToken());
        //必填项
        params.put("model.id", data.id + "");
        params.put("model.pointName", data.name);
        params.put("model.lng", data.lng + "");
        params.put("model.lat", data.lat + "");
        params.put("model.provinceName", data.provinceName);
        params.put("model.cityName", data.cityName);
        params.put("model.countyName", data.countyName);
        params.put("model.address", data.address);
        params.put("model.isOpening", data.isOpening ? "true" : "false");
        params.put("model.parkingType", data.parkingType + "");
        params.put("model.isFree", data.isFree ? "true" : "false");
        params.put("model.chargeMethod", data.chargeMethod + "");
        params.put("model.operatorId", data.operatorId + "");
        params.put("model.isEnable", data.isEnable ? "true" : "false");
        //选填项（慢充、快充、超级充）
        if (data.count4Slow > 0) {
            params.put("model.count4Slow", data.count4Slow + "");
            params.put("model.chargeModel_Slow", data.chargeModel_Slow + "");
            params.put("model.power_Slow", data.power_Slow + "");
            if (data.electricityPriceInfo_Slow != null) {
                params.put("model.electricityPriceInfo_Slow", data.electricityPriceInfo_Slow);
            }
        }
        if (data.count4Fast > 0) {
            params.put("model.count4Fast", data.count4Fast + "");
            params.put("model.chargeModel_Fast", data.chargeModel_Fast + "");
            params.put("model.power_Fast", data.power_Fast + "");
            if (data.electricityPriceInfo_Fast != null) {
                params.put("model.electricityPriceInfo_Fast", data.electricityPriceInfo_Fast);
            }
        }
        if (data.count4Super > 0) {
            params.put("model.count4Super", data.count4Super + "");
            params.put("model.chargeModel_Super", data.chargeModel_Super + "");
            params.put("model.power_Super", data.power_Super + "");
            if (data.electricityPriceInfo_Super != null) {
                params.put("model.electricityPriceInfo_Super", data.electricityPriceInfo_Super);
            }
        }
        if (data.contactNum != null) {
            params.put("model.contactNum", data.contactNum);
        }
        if (data.notes != null) {
            params.put("model.notes", data.notes);
        }

        Map<String, String> headers = new HashMap<>();

        PostFormBuilder build = OkHttpUtils.post()
                .url(URL)
                .params(params)
                .headers(headers);
        if (pic1 != null) {
            build.addFile("image1", new Date().getTime() + "_1.jpg", pic1);
        }
        if (pic2 != null) {
            build.addFile("image2", new Date().getTime() + "_2.jpg", pic2);
        }
        if (pic3 != null) {
            build.addFile("image3", new Date().getTime() + "_3.jpg", pic3);
        }
        RequestCall request = build.build();
        request.execute(callback);
        return request;
    }


}
